#include "MovingObject.h"

void MovingObject::setPosition(Vector3D pos)
{ 
	v3Position = pos;
	D3DXMatrixTranslation(&d3dPosition, v3Position.getX(), v3Position.getY(), v3Position.getZ());
}	

void MovingObject::setPosition(float x, float y, float z)
{ 
	v3Position.setCoord(x, y, z);
	D3DXMatrixTranslation(&d3dPosition, v3Position.getX(), v3Position.getY(), v3Position.getZ()); 
}

void MovingObject::setTarget(float x, float y, float z)
{ 
	v3Target.setCoord(x, y, z); 
	D3DXMatrixTranslation(&d3dTarget, v3Target.getX(), v3Target.getY(), v3Target.getZ());
}

void MovingObject::setTarget(Vector3D tar)
{
	v3Target = tar; 
	D3DXMatrixTranslation(&d3dTarget, v3Target.getX(), v3Target.getY(), v3Target.getZ());
}

void MovingObject::setAngle(float theta)
{ 
	fAngle = theta;
	D3DXMatrixRotationZ(&d3dPosition, theta);
}

void MovingObject::Update(float dt)
{
	//disallows angle to go above 2*pi or below 0
	if(fAngle < 0)
		fAngle += 2 * PI;
	else if(fAngle >= 360)
		fAngle -= 2 * PI;

	//rotates object to the desired angle while going through the position update
	if(fabs(fDesiredAngle - fAngle) > .1)
	{
		if(fDesiredAngle < fAngle)
			fAngle -= .04f;
		else
			fAngle += .04f;
		D3DXMatrixRotationZ(&d3dAngle, -1 * fAngle);
	}
	if(!this->isAtTarget())
	{
		v3Velocity = v3Velocity + v3Accel * dt;
		v3Position = v3Position + v3Velocity * dt;

		//after finding the position in our Vector3D format, d3dPosition is given a translation matrix version of it
		D3DXMatrixTranslation(&d3dPosition, v3Position.getX(), v3Position.getY(), v3Position.getZ());
	}
}

void MovingObject::Calculate()
{
	mySteering.Seek();
	//mySteering.Flee(); // THIS IS FOR CALCULATING THE FLEE BEHAVIOUR
	//mySteering.Evade();
}

bool MovingObject::isAtTarget()
{
	if(v3Position.DistanceTo(v3Target) < .1)
		return true;
	
	return false;
}